package com.whnk.attendance.service.impl;

import java.util.Date;
import java.util.List;

import com.whnk.attendance.domain.SocialInsuranceInfo;
import com.whnk.attendance.vo.SocialInsuranceInfoQueryVo;
import com.whnk.attendance.vo.SocialInsuranceInfoListVo;
import com.whnk.attendance.vo.SocialInsuranceInfoUpdateVo;
import com.whnk.attendance.vo.SocialInsuranceListVo;
import com.whnk.common.core.exception.ServiceException;
import com.whnk.common.core.utils.DateUtils;
import com.whnk.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.whnk.attendance.mapper.SocialInsuranceInfoMapper;
import com.whnk.attendance.service.ISocialInsuranceInfoService;

/**
 * 社保基本信息Service业务层处理
 * 
 * @author whnk
 * @date 2021-12-10
 */
@Service
public class SocialInsuranceInfoServiceImpl implements ISocialInsuranceInfoService 
{
    @Autowired
    private SocialInsuranceInfoMapper socialInsuranceInfoMapper;

    /**
     * 查询社保基本信息
     *
     * @param socialInsId 社保基本信息主键
     * @return 社保基本信息
     */
    @Override
    public SocialInsuranceInfo selectSocialInsuranceInfoBySocialInsId(Long socialInsId)
    {
        return socialInsuranceInfoMapper.selectSocialInsuranceInfoBySocialInsId(socialInsId);
    }

    /**
     * 查询社保基本信息列表
     *
     * @param socialInsuranceInfo 社保基本信息
     * @return 社保基本信息
     */
    @Override
    public List<SocialInsuranceInfo> selectSocialInsuranceInfoList(SocialInsuranceInfo socialInsuranceInfo)
    {
        return socialInsuranceInfoMapper.selectSocialInsuranceInfoList(socialInsuranceInfo);
    }

    /**
     * 新增社保基本信息
     *
     * @param socialInsuranceInfo 社保基本信息
     * @return 结果
     */
    @Override
    public int insertSocialInsuranceInfo(SocialInsuranceInfo socialInsuranceInfo)
    {
        socialInsuranceInfo.setCreateTime(DateUtils.getNowDate());
        return socialInsuranceInfoMapper.insertSocialInsuranceInfo(socialInsuranceInfo);
    }

    /**
     * 修改社保基本信息
     *
     * @param socialInsuranceInfo 社保基本信息
     * @return 结果
     */
    @Override
    public int updateSocialInsuranceInfo(SocialInsuranceInfo socialInsuranceInfo)
    {
        socialInsuranceInfo.setUpdateTime(DateUtils.getNowDate());
        return socialInsuranceInfoMapper.updateSocialInsuranceInfo(socialInsuranceInfo);
    }

    /**
     * 批量删除社保基本信息
     *
     * @param socialInsIds 需要删除的社保基本信息主键
     * @return 结果
     */
    @Override
    public int deleteSocialInsuranceInfoBySocialInsIds(Long[] socialInsIds)
    {
        return socialInsuranceInfoMapper.deleteSocialInsuranceInfoBySocialInsIds(socialInsIds);
    }

    /**
     * 删除社保基本信息信息
     *
     * @param socialInsId 社保基本信息主键
     * @return 结果
     */
    @Override
    public int deleteSocialInsuranceInfoBySocialInsId(Long socialInsId)
    {
        return socialInsuranceInfoMapper.deleteSocialInsuranceInfoBySocialInsId(socialInsId);
    }

    /**
     * 分页查询社保列表
     */
    public List<SocialInsuranceInfoListVo> selectSocialInsuranceInfoListVoList(SocialInsuranceInfoQueryVo socialInsuranceInfoQueryVo){
        return socialInsuranceInfoMapper.selectSocialInsuranceInfoListVoList(socialInsuranceInfoQueryVo);
    }

    /**
     * 根据逻辑主键查询社保信息
     */
    public SocialInsuranceInfoListVo selectSocialInsuranceInfoListVoBySocialInsId(Long socialInsId){
        return socialInsuranceInfoMapper.selectSocialInsuranceInfoListVoBySocialInsId(socialInsId);
    }

    /**
     * 根据社保分类编号查询社保信息
     */
    public SocialInsuranceInfoListVo selectSocialInsuranceInfoListVoBySocialInsNum(String socialInsNum){
        return socialInsuranceInfoMapper.selectSocialInsuranceInfoListVoBySocialInsNum(socialInsNum);
    }

    /**
     * 校验社保分类编号是否唯一
     */
    public boolean checkSocialInsNum(SocialInsuranceInfoUpdateVo socialInsuranceInfoUpdateVo){
        SocialInsuranceInfoListVo info = socialInsuranceInfoMapper.checkSocialInsNum(socialInsuranceInfoUpdateVo.getSocialInsNum());
        if(StringUtils.isNotNull(info) && info.getSocialInsId() != socialInsuranceInfoUpdateVo.getSocialInsId()){
            return false;
        }
        return true;
    }

    /**
     * 新增社保信息
     */
    public int insertSocialInsuranceInfo(SocialInsuranceInfoUpdateVo socialInsuranceInfoUpdateVo){
        SocialInsuranceInfo socialInsuranceInfo = new SocialInsuranceInfo();
        Date date = new Date();
        socialInsuranceInfo.setSocialInsId(socialInsuranceInfoUpdateVo.getSocialInsId());
        socialInsuranceInfo.setSocialInsNum(socialInsuranceInfoUpdateVo.getSocialInsNum());
        socialInsuranceInfo.setSocialInsBase(socialInsuranceInfoUpdateVo.getSocialInsBase());
        socialInsuranceInfo.setSocialInsRatio(socialInsuranceInfoUpdateVo.getSocialInsRatio());
        socialInsuranceInfo.setHousingFundBase(socialInsuranceInfoUpdateVo.getHousingFundBase());
        socialInsuranceInfo.setHousingFundRatio(socialInsuranceInfoUpdateVo.getHousingFundRatio());
        socialInsuranceInfo.setServiceCharge(socialInsuranceInfoUpdateVo.getServiceCharge());
        socialInsuranceInfo.setCreateTime(date);
        return socialInsuranceInfoMapper.insertSocialInsuranceInfo(socialInsuranceInfo);
    }

    /**
     * 修改社保信息
     */
    public int updateSocialInsuranceInfo(SocialInsuranceInfoUpdateVo socialInsuranceInfoUpdateVo){
        SocialInsuranceInfo socialInsuranceInfo = new SocialInsuranceInfo();
        Date date = new Date();
        socialInsuranceInfo.setSocialInsId(socialInsuranceInfoUpdateVo.getSocialInsId());
        socialInsuranceInfo.setSocialInsNum(socialInsuranceInfoUpdateVo.getSocialInsNum());
        socialInsuranceInfo.setSocialInsBase(socialInsuranceInfoUpdateVo.getSocialInsBase());
        socialInsuranceInfo.setSocialInsRatio(socialInsuranceInfoUpdateVo.getSocialInsRatio());
        socialInsuranceInfo.setHousingFundBase(socialInsuranceInfoUpdateVo.getHousingFundBase());
        socialInsuranceInfo.setHousingFundRatio(socialInsuranceInfoUpdateVo.getHousingFundRatio());
        socialInsuranceInfo.setServiceCharge(socialInsuranceInfoUpdateVo.getServiceCharge());
        socialInsuranceInfo.setUpdateTime(date);
        return socialInsuranceInfoMapper.updateSocialInsuranceInfo(socialInsuranceInfo);
    }

    /*
     * 导入社保信息
     */
    public String importData(List<SocialInsuranceInfoListVo> socialInsuranceInfoList, Boolean isUpdateSupport){
        if (StringUtils.isNull(socialInsuranceInfoList) || socialInsuranceInfoList.size() == 0)
        {
            throw new ServiceException("导入社保信息不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SocialInsuranceInfoListVo socialInsuranceInfoListVo : socialInsuranceInfoList)
        {
            try
            {
                // 验证是否存在这个社保信息
                SocialInsuranceInfoListVo socialInsurance = socialInsuranceInfoMapper.selectSocialInsuranceInfoListVoBySocialInsNum(socialInsuranceInfoListVo.getSocialInsNum());
                if (StringUtils.isNull(socialInsurance))
                {
                    SocialInsuranceInfoUpdateVo socialInsuranceInfoUpdateVo = new SocialInsuranceInfoUpdateVo();
                    socialInsuranceInfoUpdateVo.setSocialInsNum(socialInsuranceInfoListVo.getSocialInsNum());
                    socialInsuranceInfoUpdateVo.setSocialInsBase(socialInsuranceInfoListVo.getSocialInsBase());
                    socialInsuranceInfoUpdateVo.setSocialInsRatio(socialInsuranceInfoListVo.getSocialInsRatio());
                    socialInsuranceInfoUpdateVo.setHousingFundBase(socialInsuranceInfoListVo.getHousingFundBase());
                    socialInsuranceInfoUpdateVo.setHousingFundRatio(socialInsuranceInfoListVo.getHousingFundRatio());
                    socialInsuranceInfoUpdateVo.setServiceCharge(socialInsuranceInfoListVo.getServiceCharge());
                    this.insertSocialInsuranceInfo(socialInsuranceInfoUpdateVo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、社保 " + socialInsuranceInfoListVo.getSocialInsNum() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    SocialInsuranceInfoUpdateVo socialInsuranceInfoUpdateVo = new SocialInsuranceInfoUpdateVo();
                    //为修改对象的社保主键赋值
                    socialInsuranceInfoUpdateVo.setSocialInsId(socialInsurance.getSocialInsId());
                    socialInsuranceInfoUpdateVo.setSocialInsNum(socialInsuranceInfoListVo.getSocialInsNum());
                    socialInsuranceInfoUpdateVo.setSocialInsBase(socialInsuranceInfoListVo.getSocialInsBase());
                    socialInsuranceInfoUpdateVo.setSocialInsRatio(socialInsuranceInfoListVo.getSocialInsRatio());
                    socialInsuranceInfoUpdateVo.setHousingFundBase(socialInsuranceInfoListVo.getHousingFundBase());
                    socialInsuranceInfoUpdateVo.setHousingFundRatio(socialInsuranceInfoListVo.getHousingFundRatio());
                    socialInsuranceInfoUpdateVo.setServiceCharge(socialInsuranceInfoListVo.getServiceCharge());
                    this.updateSocialInsuranceInfo(socialInsuranceInfoUpdateVo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、社保 " + socialInsuranceInfoListVo.getSocialInsNum() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、社保 " + socialInsuranceInfoListVo.getSocialInsNum() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、社保 " + socialInsuranceInfoListVo.getSocialInsNum() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 逻辑主键id，社保分类编号name接口
     */
    public List<SocialInsuranceListVo> socialInsuranceList(){
        return socialInsuranceInfoMapper.socialInsuranceList();
    }
}
